package com.lyhlmj.oop_client.Frames.LoginAndRegister.RegisterFrame;

import com.lyhlmj.oop_client.Style.Buttons.RegisterButton.BackButton;
import com.lyhlmj.oop_client.Style.Buttons.RegisterButton.RegisterButton;
import com.lyhlmj.oop_client.Utils.GetScreenSize;

import javax.swing.*;
import java.awt.*;

public class RegisterPanel extends JPanel {
    private Window owner;  //要阻塞的窗口
    private RegisterButton registerButton;
    private BackButton backButton;
    private JTextField username;
    private JPasswordField password, confirmPassword;
    //  得到Frame的大小
    int width = GetScreenSize.getFitSize()[0];
    int height = GetScreenSize.getFitSize()[1];

    //    注册面板的构造函数
    public RegisterPanel(Window owner) {
        this.setOpaque(false);
        this.owner = owner;  //设置阻塞窗口为其父窗口
        this.setLayout(new GridLayout(4, 1,(int)(0.08 * height),(int)(0.08 * height)));  //设置布局形式
        this.setBorder(BorderFactory.createEmptyBorder((int)(0.4 * height), 0, 0, 0));//添加标题边框
        this.initialRegisterPanel();  //调用初始化方法
    }

    //     初始化注册面板
    private void initialRegisterPanel() {
        JLabel usernameLabel = new JLabel("请 输 入 用 户 名",SwingConstants.RIGHT);
        JLabel passwordLabel = new JLabel("请 输 入 密 码",SwingConstants.RIGHT);
        JLabel confirmPasswordLabel = new JLabel("请 确 认 密 码",SwingConstants.RIGHT);
        usernameLabel.setFont(new Font("隶书",1,40));
        passwordLabel.setFont(new Font("隶书",1,40));
        confirmPasswordLabel.setFont(new Font("隶书",1,40));
        username = new JTextField(20);
        password = new JPasswordField(20);
        confirmPassword = new JPasswordField(20);
        registerButton = new RegisterButton("注  册", owner);
        backButton = new BackButton("返  回", owner);

        this.add(usernameLabel,BorderLayout.SOUTH);
        this.add(username);
        this.add(passwordLabel);
        this.add(password);
        this.add(confirmPasswordLabel);
        this.add(confirmPassword);
        this.add(registerButton);
        this.add(backButton);
    }

    //    添加按钮的点击事件
    public void setButtonFunction() {
        registerButton.addActionListener(e -> {  //添加按钮单击事件（lambda表达式形式
            registerButton.doRegister(username.getText(), password.getText(), confirmPassword.getText());
        });
        backButton.addActionListener(e -> {  //添加按钮单击事件（lambda表达式形式
            backButton.backLoginFrame();
        });
    }
}
